package com.tanhua.interceptor;//package com.tanhua.interceptor;
//
//import cn.hutool.core.util.StrUtil;
//import com.tanhua.domain.Admin;
//import com.tanhua.service.AdminService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.web.servlet.HandlerInterceptor;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//@Component
//public class tokenInterceptor implements HandlerInterceptor {
//
//    @Autowired
//    private AdminService adminService;
//
//
//    @Override  //在controller方法之前执行
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//
//        // 1 通过请求头Authorization获取token
//        String token = request.getHeader("Authorization");
//        //由于前段每次传递过来的token都多出来了一些前缀
//        //eyJhbGciOiJIUzI1NiJ9.eyJtb2JpbGUiOnsiY3JlYXRlZCI6MTU5OTQ3MjUyODAwMCwidXBkYXRlZCI6MTU5OTQ3MjUyODAwMCwiaWQiOjEsInVzZXJuYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6ImUxMGFkYzM5NDliYTU5YWJiZTU2ZTA1N2YyMGY4ODNlIiwiYXZhdGFyIjpudWxsLCJ0b2tlbiI6bnVsbH19.nUrtF3bTdbDapbu_tP4hGNE6q2K1ZbxpRKq4p54ivxs
//        if (token != null) { //截掉浏览器带过来tonken的多余内容
//            token = token.replace("Bearer ", "");
//        }
//
//        // 2 判断app是否携带了token
//        if (StrUtil.isEmpty(token)) {
//            response.setStatus(401); //权限不足
//            return false;
//        }
//        //3 根据token获取redis中的User
//        Admin admin = adminService.tokenfromUser(token);
//        admin.setToken(token); //token存在admin对象的属性中
//        //4 判断token中是否有User
//        if (admin == null) {
//            response.setStatus(401);
//            return false;
//        }
//        //5 将User存在我们的ThreadLocal
//        AdminHolder.saveAdmin(admin);
//
//        // 放行
//        return true;
//    }
//
//    @Override  //controller结束要执行的
//    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//
//        // 将User从threadlocal删除
//        AdminHolder.remove();
//    }
//
//
//}
